import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { NzMessageService, NzModalRef, UploadFile } from 'ng-zorro-antd';
import { FormBuilder, FormControl, FormGroup,  Validators } from '@angular/forms';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { UtilService } from '@core/services/util.service';
@Component({
  selector: 'app-user-move',
  templateUrl: './user-move.component.html',
  styles: []
})
export class UserMoveComponent implements OnInit {
  @ViewChild('treedom',{ static: false }) treedom:any;
  @Input()
  set param(value:any){
    console.log(value);
    if(!value) return;
    this._param=value;
  }
  value:any;
  _param:any;
  nodes=[];
  newRoomId:number | string = "";
  validateForm!: FormGroup;
  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private util: UtilService,
    private fb: FormBuilder
  ) { }

  ngOnInit() {
    this.getHouses();
    this.validateForm = this.fb.group({
      infoName: [this._param.infoName, [Validators.required]],
      addressNow: [this._param.areaName+this._param.roomName, [Validators.required]],
    });
  }
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }

  submitForm(data) {
    console.log(data);
    if(this.newRoomId == ""){
      this.msg.error("请选择房间!");
      return;
    }
    const body={
      "personId":this._param.id,
      "oldRoom":this._param.roomId,
      "newRoom":this.newRoomId
    }
    console.log(body);
  this.http.post(this.api.doorCard.relocateHouse.url,body).subscribe((res:any) => {
    console.log(res)
    if (res.status === '200') {
      this.msg.success('操作成功');
      this.modalRef.triggerOk();
    } else {
      this.msg.error(res.message)
    }
  })
}

//点击下拉菜单节点
onChange($event: string): void {
  let data=this.treedom.getTreeNodeByKey($event);
  let id=data.origin.id;
  let pId=data.origin.pId;
  if(pId == ""){
    this.msg.error("请选择房间!");
    this.newRoomId="";
    return;
  }
  this.newRoomId=id;
}
 

    //获取楼栋房间列表
    getHouses(): void{
      this.http.post(this.api.manager.queryUnitHouse.url,{"areaParentAllName":""}).subscribe( (res:any) => {
        console.log(res);
        if (res.status === '200' && res.data && res.data.list.length > 0) {
          let list=[];
          for(let item of res.data.list){
            list.push({
              key:item.id,
              pId:item.areaId == undefined ? "" : item.areaId,
              title:item.areaParentAllName,
              id:item.id,
              value:item.id
            })
            for(let current of item.roomList){
              list.push({
                key:current.id,
                pId:current.areaId,
                title:""+current.roomNumber,
                id:current.id,
                value:current.id
              })
            }
          }
          // console.log(list);
          console.log(this.util.getTreeNodeOption(list));
          this.nodes=this.util.getTreeNodeOption(list);
        }
      })
    }

}
